package com.wxtbk.service;

import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.WirelessShareTpwdCreateRequest;
import com.taobao.api.request.WirelessShareTpwdCreateRequest.IsvTpwdInfo;
import com.taobao.api.response.WirelessShareTpwdCreateResponse;
import com.wxtbk.model.WtItem;
import com.fanerma.model.*;
import com.wxtbk.utils.*;

public class TbkService {

	String apiurl = "http://gw.api.taobao.com/router/rest";
	String appkey = "23549659";
	String secret = "4d1567d0efd1eb9d3f29538b66e04d99";
	// 推广渠道的标识，mm开头的三段式
	String pid = "11297933_19228929_67120243";

	public String GenTaokoulin(String  id) {
		
		WtItem p = WtItem.dao.findById(id);
		//TbkProduct p = TbkProduct.dao.findFirst("select * from TbkProduct where item_id = ? limit 1", id);
		if (p == null) {
			LogKit.warn("taobao id not found." + id);
			return null;
		}
		
		String itemid = p.getItemid();
		String title = p.getItemtitle();
		String imgurl = p.getItempicurl();
		String activeid = p.getCouponId();
		
		/*
		if ( p.getStr("taokoulin") == null ){
			
			try {
				String url = GenTwoOne(itemid,activeid);
				String taokoulin =  GenTaokoulin(imgurl, url);
				
				//必须设置主键，否则每次都是新增。
				//p.set("taokoulin", taokoulin).update();
				
				
				
			} catch (ApiException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		*/

		
		try {
			String url = GenTwoOne(itemid,activeid);
			String taokoulin =  GenTaokoulin(imgurl, url);
			p.setTaokoulin(taokoulin);
			p.update();
			return p.getTaokoulin();
			
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * 生成二合一链接(优惠券+淘宝客)
	 */
	private String GenTwoOne(String itemid,String activeid) {
		// 券的标识，可从商家给到的优惠券url中获得
		//String activeid = "fb95f725419343a59d13808bad933ea4";
		// 商品的标识，可从商家处获得，也可从商品详情页的url中获得
		//String itemid = "40054719858";
		// 是否强制定向（选填），决定了商品的结算佣金；当输入dx=1时，按当前pid报名通过的定向计划佣金进行结算，如果没有定向计划按通用佣金结算；当未输入时，则首先在鹊桥里寻找该商品，如果存在则按鹊桥结算；如果不存在则寻找定向计划按定向计划的佣金结算，如果均不存在则按通用佣金结算
		String dx = "1";
		String mergeurl = "http://uland.taobao.com/coupon/edetail?activityId=" + activeid + "&pid=mm_" + pid
				+ "&itemId=" + itemid + "&src= pgy_pgyqf &dx=" + dx;
		return mergeurl;
	}

	/// 生成淘口令
	private String GenTaokoulin(String imgurl, String url) throws ApiException {
		TaobaoClient client = new DefaultTaobaoClient(apiurl, appkey, secret, "json");
		WirelessShareTpwdCreateRequest req = new WirelessShareTpwdCreateRequest();
		IsvTpwdInfo obj1 = new IsvTpwdInfo();
		obj1.setExt("{\"xx\":\"xx\"}");
		obj1.setLogo(imgurl);
		obj1.setText("超值活动，惊喜活动多多");
		obj1.setUrl(url);
		obj1.setUserId(24234234234L);
		req.setTpwdParam(obj1);
		WirelessShareTpwdCreateResponse rsp = client.execute(req);
		return rsp.getModel();

	}


	public String FindCoupon(String tburl){
		String url = "http://www.quanduoduo.com/index";
		String param = "q="+tburl;
		HttpRequest.sendPost(url, param);
		return null;
	}
	
	
}

